﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Layer.Connection;
using System.Data;
using STT.Business;
using STT.Entities;
namespace STT.Profile.UserControls
{
    public partial class Index : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInfoHV();
        }
        private void LoadInfoHV()
        {
            DtlIndex.DataSource = tb.GetDataTableById2("Get_InfoHocVien_byMahv", Session["Mahv"].ToString());
            DtlIndex.DataBind();
        }
        protected void DtlIndex_CancelCommand(object source, DataListCommandEventArgs e)
        {

            DtlIndex.EditItemIndex = -1;
            LoadInfoHV();
         
        }

        protected void DtlIndex_DeleteCommand(object source, DataListCommandEventArgs e)
        {

        }

        protected void DtlIndex_EditCommand(object source, DataListCommandEventArgs e)
        {
            DtlIndex.EditItemIndex = e.Item.ItemIndex;
            LoadInfoHV();
        }

        protected void DtlIndex_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
               if (e.CommandName.Equals("Update"))
                {
                    ETbHocVien hv = new ETbHocVien();
                    hv.MaHV = DtlIndex.DataKeys[e.Item.ItemIndex].ToString();
                    TextBox txtngaysinh = (TextBox)e.Item.FindControl("txtngaysinh");
                    TextBox txttenhv = (TextBox)e.Item.FindControl("txtten");
                    TextBox txtho = (TextBox)e.Item.FindControl("txtho");
                    TextBox txtquequan = (TextBox)e.Item.FindControl("txtquequan");
                    TextBox txtdiachi = (TextBox)e.Item.FindControl("txtdiachi");
                    TextBox txtsdt = (TextBox)e.Item.FindControl("txtsdt");
                    TextBox txtghichu = (TextBox)e.Item.FindControl("txtghichu");
                    TextBox txtanh = (TextBox)e.Item.FindControl("txtanh");
                    FileUpload fileup = (FileUpload)e.Item.FindControl("Fulanh");
                    if (fileup.HasFile)
                    {
                        
                           if (System.IO.File.Exists(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text))
                                System.IO.File.Delete(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text);
                            string id = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + fileup.FileName;
                            fileup.SaveAs(Server.MapPath("~/Uploads/Avatar/") + id);
                            hv.Anh = id;
                            hv.MaHV = DtlIndex.DataKeys[e.Item.ItemIndex].ToString();
                            hv.NgaySinh = Convert.ToDateTime(txtngaysinh.Text);
                            hv.TenHV = txtho.Text;
                            hv.Ten = txttenhv.Text;
                            hv.QueQuan = txtquequan.Text;
                            hv.DiaChi = txtdiachi.Text;
                            hv.Sdt = txtsdt.Text;
                            hv.GhiChu = txtghichu.Text;
                            Business.BTbHocVien.UpdateInfoHV(hv);
                       
                    }
                    else
                    {
                        hv.MaHV = DtlIndex.DataKeys[e.Item.ItemIndex].ToString();
                        hv.NgaySinh = Convert.ToDateTime(txtngaysinh.Text);
                        hv.TenHV = txtho.Text;
                        hv.Ten = txttenhv.Text;
                        hv.QueQuan = txtquequan.Text;
                        hv.DiaChi = txtdiachi.Text;
                        hv.Sdt = txtsdt.Text;
                        hv.GhiChu = txtghichu.Text;
                        hv.Anh = txtanh.Text;
                        Business.BTbHocVien.UpdateInfoHV(hv);
                    }
                    DtlIndex.EditItemIndex = -1;
                    Response.Redirect("Default.aspx?mod=TTCN117");
                }
            }
            catch { }
        }

        protected void DtlIndex_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item)
                {

                    DataRowView drv = e.Item.DataItem as DataRowView;
                    DataList innerDataList = e.Item.FindControl("dtlngoaingu") as DataList;
                    innerDataList.DataSource = tb.GetDataTableById2("GetNgoaiNgu_ByMahv", Session["Mahv"].ToString());
                    innerDataList.DataBind();

                    DataList innerDataList2 = e.Item.FindControl("dtlbangcap") as DataList;
                    innerDataList2.DataSource = tb.GetDataTableById2("GetBangCap_ByMahv", Session["Mahv"].ToString());
                    innerDataList2.DataBind();
                }
            }
            catch { }
        }
    }
}